import type { User } from '@/types/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'
// import Cookies from 'js-cookie'

export const useUserStore = defineStore(
  'user',
  () => {
    // ref 内部也是用的 reactive，只不过用 reactive 转换后的 Proxy 放到了 .value 属性上
    const user = ref<User>()

    // ref({ }) ==> reactive({ }) ==> Proxy({})
    // ref<number>(1) ==>  const obj = {}  ==> Object.defineProperty (obj, 'value', { get, set })

    const setUser = (u: User) => {
      user.value = u
    }

    const delUser = () => {
      user.value = undefined
    }

    const count = ref(0)

    const addCount = () => {
      count.value++
    }

    // 记录回跳地址
    const returnUrl = ref('')
    const setReturnUrl = (url: string) => (returnUrl.value = url)

    return { user, setUser, delUser, count, addCount, returnUrl, setReturnUrl }
  },
  {
    persist: {
      paths: ['user'],
      // storage: {
      //   getItem(k) {
      //     Cookies.get(k)
      //   },
      //   setItem(k, v) {
      //     Cookies.set(k, v)
      //   },
      // },
    },
  }
)

// export const aaa = 1

// export default 111
